package com.logos.blog.gateway.auth;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author logos
 * date 2025/7/1 21:10
 * @version v1.0
 * @Package com.logos.blog.gateway.auth
 */
@Configuration
@Slf4j
public class SaTokenConfigure {

    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
                // 开放地址
                .addExclude("/**")
                .setError(this::getSaResult);
    }

    private SaResult getSaResult(Throwable throwable) {
        if (throwable instanceof NotLoginException) {
            log.error("请先登录");
            return SaResult.error("请先登录");
        } else if (throwable instanceof NotRoleException notRoleException) {
            // TODO 补充用户权限
            log.error("您无权限进行此操作！");
            return SaResult.error("您无权限进行此操作！");
        } else if (throwable instanceof NotPermissionException notPermissionException) {
            // TODO 补充手机号权限
            log.error("您无权限进行此操作！");
            return SaResult.error("您无权限进行此操作！");
        } else {
            return SaResult.error(throwable.getMessage());
        }
    }
}
